package com.funny.stock.trade.service;

import com.funny.stock.base.util.PropertiesCopyUtil;
import com.funny.stock.data.vo.StockConstants;
import com.funny.stock.trade.dao.pojo.DayTradePlanDO;
import com.funny.stock.trade.dao.repo.DayTradePlanRepo;
import com.funny.stock.trade.vo.DayTradePlanVO;
import com.funny.stock.trade.vo.PlanStatus;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 *日内T0交易服务
 *
 * @author pengl
 * @date 2020/11/16
 */
@Component
public class DayTradePlanServiceImpl implements DayTradePlanService {
    private static final Logger logger = LoggerFactory.getLogger(DayTradePlanServiceImpl.class);

    @Autowired
    DayTradePlanRepo repository;

    @Cacheable(value = StockConstants.KEY_DAY_TRADE_POOL_LIST, unless= "#result.size() == 0")
    @Override
    public List<DayTradePlanVO> queryTradingPlanList() {
        logger.info("queryTradingPlanList");
        List<DayTradePlanDO> list = repository.findByStatusEquals(PlanStatus.TRADING.getStatus());
        return PropertiesCopyUtil.copyPropertiesByList(list, DayTradePlanVO.class);
    }

    @Override
    public List<DayTradePlanVO> queryTradePlanList(Integer personId,Long composeId) {
        List<DayTradePlanDO> list = repository.findByCreateIdAndComposeIdAndIsDelete(personId.longValue(),composeId, 0);
        return PropertiesCopyUtil.copyPropertiesByList(list, DayTradePlanVO.class);
    }

    @Override
    @Transactional
    public DayTradePlanVO add(DayTradePlanVO object) {
        logger.info("add:{}",object.getCode());
        object.setIsDelete(0);
        object.setStatus(PlanStatus.CONFIG.getStatus());
        DayTradePlanDO planDO = PropertiesCopyUtil.copyProperties(object, DayTradePlanDO.class);
        planDO = repository.save(planDO);
        object.setId(planDO.getId());
        return object;
    }

    @Cacheable(value = StockConstants.KEY_DAY_TRADE_POOL, key = "#id", unless="#result == null")
    @Override
    public DayTradePlanVO findByIdAndCreateId(Long id, Long createId) {
        DayTradePlanDO planDO = repository.findByIdAndCreateId(id,createId);
        DayTradePlanVO planVO = PropertiesCopyUtil.copyProperties(planDO, DayTradePlanVO.class);
        return planVO;
    }

    @Caching(
            put = {@CachePut(value = StockConstants.KEY_DAY_TRADE_POOL, key = "#id", condition = "#result != null ")},
            evict = {@CacheEvict(value = StockConstants.KEY_DAY_TRADE_POOL_LIST, allEntries = true, condition = "#result != null ")}
    )
    @Transactional
    @Override
    public DayTradePlanVO startPlan(Long id, Integer personId) {
        DayTradePlanDO planDO = repository.findByIdAndCreateId(id, Long.valueOf(personId));
        if(planDO == null || planDO.getIsDelete() == 1) return null;

        planDO.setStatus(PlanStatus.TRADING.getStatus());

        planDO.setUpdateId(Long.valueOf(personId));
        planDO.setUpdateTime(new Date());
        planDO.setStartTime(new Date());

        planDO = repository.save(planDO);
        DayTradePlanVO planVO = PropertiesCopyUtil.copyProperties(planDO, DayTradePlanVO.class);
        return planVO;
    }

    @Caching(evict = {
            @CacheEvict(value = StockConstants.KEY_DAY_TRADE_POOL, key = "#id", condition = "#result != null "),
            @CacheEvict(value = StockConstants.KEY_DAY_TRADE_POOL_LIST, allEntries = true, condition = "#result != null ")
    })
    @Transactional
    @Override
    public DayTradePlanVO stopPlan(Long id, Integer personId) {
        DayTradePlanDO planDO = repository.findByIdAndCreateId(id, Long.valueOf(personId));
        if(planDO == null || planDO.getIsDelete() == 1) return null;

        planDO.setStatus(PlanStatus.STOP.getStatus());

        planDO.setUpdateId(Long.valueOf(personId));
        planDO.setUpdateTime(new Date());
        planDO = repository.save(planDO);
        DayTradePlanVO planVO = PropertiesCopyUtil.copyProperties(planDO, DayTradePlanVO.class);
        return planVO;
    }

    @Caching(evict = {
            @CacheEvict(value = StockConstants.KEY_DAY_TRADE_POOL, key = "#id", condition = "#result != null "),
            @CacheEvict(value = StockConstants.KEY_DAY_TRADE_POOL_LIST, allEntries = true, condition = "#result != null ")
    })
    @Override
    @Transactional
    public DayTradePlanVO remove(Long id,Integer personId) {
        DayTradePlanDO planDO = repository.findByIdAndCreateId(id, Long.valueOf(personId));
        if(planDO == null || planDO.getIsDelete() == 1) return null;

        planDO.setIsDelete(1);
        planDO.setStatus(PlanStatus.STOP.getStatus());

        planDO.setUpdateId(Long.valueOf(personId));
        planDO.setUpdateTime(new Date());
        planDO = repository.save(planDO);
        DayTradePlanVO planVO = PropertiesCopyUtil.copyProperties(planDO, DayTradePlanVO.class);
        return planVO;
    }
}
